/***************************************************************************
 创建者: 华磊
 开始时间: 2020.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2020.5.8 类的具体实现代码编写

 ***************************************************************************
 *  @file x.h
 *  功能:
 *
 *                                                                         *
 ***************************************************************************/
#ifndef TIMETOOL_H
#define TIMETOOL_H

#include <QString>
#include <QDebug>
#include <QObject>
#include <QMap>
#include <QList>
#include <QMutex>
#include <QTimer>

#define mymax(a,b) (a>b?a:b)
#define mymin(a,b) (a<b?a:b)

#define WRITE_DEBUG_DATA_FILE 0
#define D_MAX_TIME_COUNT 100

enum E_TIME{
    D_TIME_CUSTOM,
    D_TIME_MATH,
    D_TIME_RECEIVE,
    D_TIME_SEND,
    D_TIME_TIMER,

    D_TIME_MAX
};

struct S_TIME_DATA{
    int id;
    int counter;
    struct timespec t1,t2,t3,t4;
    int cycleTime;
    double nowTime;//ns
     double maxTime;//ns volatile
     double minTime;//ns volatile
    qint64 addTime;
    float averageTime;//消耗平均时间
    int printTime;
    int printInterval;
    int isPrintf;
    bool saveToFile;
    QString msg;
    int _min[3];
    int _max[3];
    qint64 maxCount[3];
    qint64 minCount[3];
  //  bool isPeriodTimeInitialed;

};

struct S_DATE_TIME{
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int second;
};

/**
 * @brief The TimeTool class 时间测量工具类
 */
class  TimeTool : public QObject
{
    Q_OBJECT
public:

    static TimeTool* getInstance();
    ~TimeTool();


public:
    int getTimeData(int timeIdIn,S_TIME_DATA** timeDataPointer);
    int creatTimer(QString msg,int cycle, int printInterval,
                   int min_v[3], int max_v[3], bool saveTofile=false);

    /**
    * @brief recordInTime 时间开始
    */
    void recordInTime(int id);
    /**
     * @brief recordOutTime　时间结束
     */
    void recordOutTime(int id);
    /**
    * @brief recordPeirodTime  周期测量
    */
    void recordPeirodTime(int id);
    /**
   * @brief reset　时间测量复位
   */
    void reset(int id);
    /**
   * @brief resetAll　时间测量复位
   */
    void resetAll();//hualei,有问题，最大值好像清除不了。

signals:
    void signal_print(int id);
public slots:
    void slot_print(int id);
    void slot_print();


private:
 QMutex mutex;
 QString file;
 QMap< int, S_TIME_DATA* > timeDataMap;
 QVector<S_TIME_DATA> timeData;

private:
   int idcount;
   explicit TimeTool( QObject* parent=0);
   static TimeTool* tTool;
   QTimer timer;
//   pthread_mutex_t timeMutex;
//   pthread_mutexattr_t attr;

};


extern void rtt_localtime(unsigned long res, S_DATE_TIME *time,  long timezone /*= 8*/ );
extern long gettime_second();
extern void printNowTime();

#endif // TIMETOOL_H
